//
//  BHCompetitionDetailModel.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/17.
//

import UIKit

class BHCompetitionDetailModel {
    var gameId: String = ""
    var name: String = ""
    var sponsor: String = "" // 主办方
    var cityName: String = ""
    var regionName: String = ""
    var startDate: String = "" // 开赛日期
    var signupEnd: String = "" // 报名截止时间
    var startTime = "" // 比赛开始时间
    var endTime: String = "" // 比赛结束时间
    var address: String = ""
    var introduce: String = ""
    var fee: String = "" // 报名费用
    var backRule: String = "" // 退改规则
    var fileUrl: String = "" // 赛事文件
    var contactNumber: String = ""
    var groupNumber: String = "" // 群聊id
    
    
    var images: [BHImageOrVideoModel] = []
    
    
    var ifAttention: Int = 0 //是否收藏 1已收藏 0未收藏
    var whetherSignin: Int = 0  // 是否需要签到
    var platformType: Int = 0  // 1商户创建 2平台创建
    var eventType: Int = 0 // 1个人赛 2团体赛（不需要报名费）
    var feeType: Int = 0  // 费用类型 0免费 1报名费 2押金
    var status: Int = 0  // 0报名已截止 1可以报名 2已报名 3赛事已结束 4报名已满
    
    // 报名明细
    var number: Int = 0 // 已报名人数
    var remain: Int = 0 // 剩余报名人数
    var urls: [String] = [] // 报名人头像
    
    
    
    
    func parseDic(_ dic: [String:Any])  {
        self.gameId = BHJsonTool.decodeStringFromDic(dic: dic, key: "id")
        self.name = BHJsonTool.decodeStringFromDic(dic: dic, key: "name")
        self.sponsor = BHJsonTool.decodeStringFromDic(dic: dic, key: "sponsor")
        self.cityName = BHJsonTool.decodeStringFromDic(dic: dic, key: "cityName")
        self.regionName = BHJsonTool.decodeStringFromDic(dic: dic, key: "regionName")
        self.startDate = BHJsonTool.decodeStringFromDic(dic: dic, key: "startDate")
        self.signupEnd = BHJsonTool.decodeStringFromDic(dic: dic, key: "signupEnd")
        self.startTime = BHJsonTool.decodeStringFromDic(dic: dic, key: "startTime")
        self.endTime = BHJsonTool.decodeStringFromDic(dic: dic, key: "endTime")
        self.address = BHJsonTool.decodeStringFromDic(dic: dic, key: "address")
        self.introduce = BHJsonTool.decodeStringFromDic(dic: dic, key: "introduce")
        self.address = BHJsonTool.decodeStringFromDic(dic: dic, key: "address")
        self.fee = BHJsonTool.decodeStringFromDic(dic: dic, key: "fee")
        self.backRule = BHJsonTool.decodeStringFromDic(dic: dic, key: "backRule")
        self.fileUrl = BHJsonTool.decodeStringFromDic(dic: dic, key: "fileUrl")
        self.contactNumber = BHJsonTool.decodeStringFromDic(dic: dic, key: "contactNumber")
        self.groupNumber = BHJsonTool.decodeStringFromDic(dic: dic, key: "groupNumber")

        self.ifAttention = BHJsonTool.decodeIntFromDic(dic: dic, key: "ifAttention")
        self.whetherSignin = BHJsonTool.decodeIntFromDic(dic: dic, key: "whetherSignin")
        self.platformType = BHJsonTool.decodeIntFromDic(dic: dic, key: "platformType")
        self.eventType = BHJsonTool.decodeIntFromDic(dic: dic, key: "eventType")
        self.feeType = BHJsonTool.decodeIntFromDic(dic: dic, key: "feeType")
        self.status = BHJsonTool.decodeIntFromDic(dic: dic, key: "status")

        let array = BHJsonTool.decodeArrayFromDic(dic: dic, key: "images")
        if self.images.count != 0 {
            self.images.removeAll()
        }
        
        for item in array {
            if item is NSNull {
                
            }else {
                let model = BHImageOrVideoModel.init()
                model.imageUrl = item as? String
                self.images.append(model)
            }
        }
    }
}


// 动态
class BHCompetitionDynModel {
    var dynId: String = ""
    var eventId: String = ""
    var content: String = ""
    var createBy: String = ""
    var createTime: String = ""
  
    var sourceArray: [BHCompetitionDynSourceModel] = []
    var imageOrVideoArray: [BHImageOrVideoModel] = []
    

    func parseDic(_ dic: [String:Any])  {
        self.dynId = BHJsonTool.decodeStringFromDic(dic: dic, key: "id")
        self.eventId = BHJsonTool.decodeStringFromDic(dic: dic, key: "eventId")
        self.content = BHJsonTool.decodeStringFromDic(dic: dic, key: "content")
        self.createBy = BHJsonTool.decodeStringFromDic(dic: dic, key: "createBy")
        self.createTime = BHJsonTool.decodeStringFromDic(dic: dic, key: "createTime")

        let array = BHJsonTool.decodeArrayFromDic(dic: dic, key: "reportList")
        for itemDic in array {
            let model = BHCompetitionDynSourceModel.init()
            model.parseDic(itemDic as! [String:Any])
            
            let comModel = BHImageOrVideoModel.init()
            comModel.sourceType = model.dataType == 1 ? 0:1
            if model.dataType == 1 { // 图片
                comModel.imageUrl = model.url
            }else {
                comModel.videoUrl = model.url
            }
            self.imageOrVideoArray.append(comModel)
            self.sourceArray.append(model)
        }
        
    }
    
    lazy var showTextAttribute: NSMutableAttributedString = {
        if self.content.isEmpty == true {
            return NSMutableAttributedString.init(string: "")
        }else {
            let attribute = self.content.toAttribute().addLineSpace(5)
            return attribute
        }
    }()
    
    lazy var height: CGFloat = {
        var h: CGFloat = 15
        if self.content.isEmpty == true {
            h = h+18
        }else {
            let textHeight = self.showTextAttribute.calculateSize(CGSize(width: UIScreen.width-32, height: CGFloat.greatestFiniteMagnitude)).height
            h = h + ceil(textHeight)
        }
        h = h+30 // 时间高度
        if self.sourceArray.count != 0 {
            let w_h = (UIScreen.width-32-14)/3.0;
            if self.sourceArray.count > 6 {
                h = h + 3 * w_h + 2 * 10
            }else if self.sourceArray.count > 3 {
                h = h + 2 * w_h + 10
            }else {
                h = h + w_h
            }
        }
        return h + 20 // 20为底部间隔
    }()
    
}

class BHCompetitionDynSourceModel {
    var sourceId: String = ""
    var businessId: String = ""
    var url: String = ""

    var type: Int = 0
    var dataType: Int = 0 // 资源类型 1图片 2视频
    
    
    func parseDic(_ dic: [String:Any])  {
        self.sourceId = BHJsonTool.decodeStringFromDic(dic: dic, key: "id")
        self.businessId = BHJsonTool.decodeStringFromDic(dic: dic, key: "businessId")
        self.url = BHJsonTool.decodeStringFromDic(dic: dic, key: "url")
        
        self.type = BHJsonTool.decodeIntFromDic(dic: dic, key: "type")
        self.dataType = BHJsonTool.decodeIntFromDic(dic: dic, key: "dataType")
    }
    
    lazy var videoCoverImage: UIImage? = {
        if self.dataType == 2 {
            let img = BHCommonTool.thumbImage(url: self.url)
            return img
        }
        return nil
    }()
}


// 相关报道
class BHCompetitionReportModel {
    var reportId: String = ""
    var eventId: String = ""
    var title: String = ""
    var content: String = ""
    var url: String = "" // 文字或者图片地址
    var createBy: String = ""
    var createTime: String = ""
    var updateBy: String = ""
    var updateTime: String = ""

    var abstracts: Int = 0 // 摘要 摘要类型 1图片 2文字

    func parseDic(_ dic: [String:Any])  {
        self.reportId = BHJsonTool.decodeStringFromDic(dic: dic, key: "id")
        self.eventId = BHJsonTool.decodeStringFromDic(dic: dic, key: "eventId")
        self.url = BHJsonTool.decodeStringFromDic(dic: dic, key: "url")
        self.title = BHJsonTool.decodeStringFromDic(dic: dic, key: "title")
        self.content = BHJsonTool.decodeStringFromDic(dic: dic, key: "content")
        self.createBy = BHJsonTool.decodeStringFromDic(dic: dic, key: "createBy")
        self.createTime = BHJsonTool.decodeStringFromDic(dic: dic, key: "createTime")
        self.updateBy = BHJsonTool.decodeStringFromDic(dic: dic, key: "updateBy")
        self.updateTime = BHJsonTool.decodeStringFromDic(dic: dic, key: "updateTime")
        
        self.abstracts = BHJsonTool.decodeIntFromDic(dic: dic, key: "abstracts")

    }
    
    lazy var showTextAttribute: NSMutableAttributedString = {
        if self.abstracts != 2 {
            return NSMutableAttributedString.init(string: "")
        }else {
            let attribute = self.url.toAttribute().addLineSpace(5)
            return attribute
        }
    }()
    
    lazy var height: CGFloat = {
        var h: CGFloat = 16+10
        if self.abstracts == 2 { // 文字摘要
            let textHeight = self.showTextAttribute.calculateSize(CGSize(width: UIScreen.width-16-103, height: CGFloat.greatestFiniteMagnitude)).height
            h = h + ceil(textHeight)
        }else if self.abstracts == 1 {
            let w = UIScreen.width-16-103
            let imgH = (100 * w) / 237.0
            h = h + ceil(imgH)
        }else {
            return 0
        }
        return h + 20 // 20为底部间隔
    }()
}
